/*
 * @Description:
 * @Author: qingyang
 * @Date: 2023-06-26 13:48:59
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-03 15:19:51
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { ILoginLogParams } from '@/apis/system/systemLog/model/logListModel'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { getLoginLog } from '@/apis/system/systemLog/logList'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'

export const useLogList = () => {
  // 搜索字段及其他
  const searchForm = reactive<ILoginLogParams>({
    operator: null,
    operateTimeBegin: null,
    operateTimeEnd: null,
    createTime: []
  })
  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '登录账号',
      field: 'operator',
      type: 'input'
    },
    {
      name: '操作日期',
      field: 'createTime',
      type: 'datePicker',
      startPlaceholder: '登录时间开始',
      endPlaceholder: '登录时间截止',
      dateType: 'daterange',
      format: 'YYYY-MM-DD'
    }
  ])
  // 表格配置
  const tableOptions: CPageTableProps['tableOptions'] = {
    loadData
  }
  function loadData(parameter: BasicPagingListParams) {
    const params: ILoginLogParams & BasicPagingListParams = Object.assign(handleParams(), parameter)
    return getLoginLog(params)
  }
  // 处理参数
  const handleParams = () => {
    // 参数
    let params: ILoginLogParams = {
      operator: searchForm.operator,
      operateTimeBegin: searchForm.createTime?.length ? searchForm.createTime[0] : undefined,
      operateTimeEnd: searchForm.createTime?.length ? searchForm.createTime[1] : undefined
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '登录账号/登录方式',
      field: 'operator',
      slot: 'operatorSlot',
      width: '300',
      columnSettingConfig: {
        enabled: true
      }
    },
    // {
    //   title: '登录方式',
    //   field: 'content',
    //   width: '130',
    //   columnSettingConfig: {
    //     enabled: true
    //   }
    // },
    {
      title: '登录参数',
      field: 'params',
      minWidth: '200',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作结果',
      field: 'gwpName',
      slot: 'operationResultSlot',
      width: '200',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: 'ip地址',
      field: 'address',
      width: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '登录时间',
      field: 'createTime',
      width: '200',
      columnSettingConfig: {
        enabled: true
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
